<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\LawOrder;
use App\Models\Question;
use App\Models\QuestionSearch;
use App\Services\QuestionService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;

class QuestionController extends Controller
{
    /**
     * 列表
     */
    public function index(Request $request)
    {
        $data            = $request->all();
        $category_one_id = $data['category_one_id'] ?? 0;
        $search          = $data['search'] ?? '';

        $categoryList = Category::query()->where('parent_id', 0)->get();
        $categoryNum  = Category::query()->where('parent_id', 0)->sum('num');

        $list = Question::query()
            ->with([
                'user'                 => function ($query) {
                    $query->select('id', 'name', 'avatar');
                },
                'lawOrder',
                'lawOrder.categoryOne' => function ($query) {
                    $query->select('id', 'title');
                },
                'lawOrder.categoryTwo' => function ($query) {
                    $query->select('id', 'title');
                },
                'comment'              => function ($query) {
                    $query->select('id', 'question_id', 'user_id')->where('parent_id', 0)->orderByDesc('id');
                },
                'comment.user'         => function ($query) {
                    $query->select('id', 'name');
                },
            ])
            ->when($search != '', function ($query) use ($search) {
                $query->where('title', 'like', '%' . $search . '%');
            })
            ->when($category_one_id > 0, function ($query) use ($category_one_id) {
                $query->whereExists(function ($query) use ($category_one_id) {
                    $query->select('law_order.question_id')
                        ->where('law_order.category_one_id', $category_one_id)
                        ->from('law_order')
                        ->whereColumn('law_order.question_id', 'question.id');
                });
            })
            ->where('status', 1)
            ->orderByDesc('id')
            ->paginate(10);
        // return ['category_list' => $categoryList, 'list' => $list];

        $list->appends(['category_one_id' => $category_one_id]);
        if ($search != '') {
            $list->appends(['search' => $search]);

            $questionSearchInfo = QuestionSearch::query()->where('search', $search)->first();
            if (!$questionSearchInfo) {
                QuestionSearch::query()->insert([
                    'search' => $search,
                    'num' => 1,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ]);
            } else {
                QuestionSearch::query()->where('id', $questionSearchInfo['id'])->update([
                    'num' => DB::raw('num + 1'),
                ]);
            }
        }

        $questionSearchList = QuestionSearch::query()->limit(10)->orderByDesc('num')->get();

        return View::make('question.list', [
            'category_list'   => $categoryList,
            'list'            => $list,
            'category_one_id' => $category_one_id,
            'search'          => $search,
            'category_num'          => $categoryNum,
            'question_search_list'          => $questionSearchList,
        ]);
    }

    /**
     * 列表
     */
    public function list(Request $request)
    {
        $data = $request->all();
        $list = Question::query()
            ->with([
                'user'                 => function ($query) {
                    $query->select('id', 'name', 'avatar');
                },
                'lawOrder',
                'lawOrder.categoryOne' => function ($query) {
                    $query->select('id', 'title');
                },
                'lawOrder.categoryTwo' => function ($query) {
                    $query->select('id', 'title');
                },
                'comment'              => function ($query) {
                    $query->select('id', 'question_id', 'user_id')->where('parent_id', 0)->orderByDesc('id');
                },
                'comment.user'         => function ($query) {
                    $query->select('id', 'name');
                },
            ])
            ->where('status', 1)
            ->whereExists(function ($query) use ($data) {
                $query->select('law_order.question_id')
                    ->where('law_order.category_one_id', $data['category_one_id'])
                    ->from('law_order')
                    ->whereColumn('law_order.question_id', 'question.id');
            })
            ->orderByDesc('id')
            ->paginate(2)
            ->toArray();

        $result = [
            'code' => 1,
            'msg'  => '',
            'data' => [
                'total' => $list['total'],
                'list'  => $list['data'],
                'page'  => "
<ul>
    <li><font class=\"page_noclick\"><em class=\"icon_page_prev\"></em></font></li>
    <li class=\"page_last\"><a href=\"javascript:;\" class=\"page_button\" data-page=\"2\" aria-label=\"page_button\"><em class=\"icon_page_next\"></em></a></li>
    <li><font class=\"page_item_current \">1</font></li>
    <li><font class=\"page_item_current \">2</font></li>
    <li><a href=\"javascript:;\" class=\"page_item\" data-page=\"2\">2</a></li>
    <li><a href=\"javascript:;\" class=\"page_item\" data-page=\"3\">3</a></li>
    <li><a href=\"javascript:;\" class=\"page_item\" data-page=\"4\">4</a></li>
    <li><a href=\"javascript:;\" class=\"page_item\" data-page=\"5\">5</a></li>
    <li><a href=\"javascript:;\" class=\"page_item\" data-page=\"6\">6</a></li>
    <li><a href=\"javascript:;\" class=\"page_item\" data-page=\"7\">7</a></li>
    <li><a href=\"javascript:;\" class=\"page_item\" data-page=\"8\">8</a></li>
    <li class=\"\"><font class=\"page_item\">...</font></li>
    <li><a href=\"javascript:;\" class=\"page_item\" data-page=\"9999\">9999</a></li>
    <li><a href=\"javascript:;\" class=\"page_item\" data-page=\"10000\">10000</a></li>
    <li class=\"page_last\"><a href=\"javascript:;\" class=\"page_button\" data-page=\"2\" aria-label=\"page_button\"><em class=\"icon_page_next\"></em></a></li>
    <li class='word l20'>前往</li>
    <input class='jump_page' name='jump_page' max='10000' link='/companies/get_enterprise_list?page=%7Bjump_page%7D' aria-label='jump_page'>
    <li class='word r20'>页</li>
    <li class='jump'><a href='javascript:;'>跳转</a></li>
</ul>",
            ],
        ];

        return $result;
    }

    /**
     * 信息
     */
    public function details(Request $request)
    {
        $data = $request->all();
        $info = Question::query()
            ->with([
                'user'                 => function ($query) {
                    $query->select('id', 'name', 'avatar');
                },
                'lawOrder',
                'lawOrder.categoryOne' => function ($query) {
                    $query->select('id', 'title');
                },
                'lawOrder.categoryTwo' => function ($query) {
                    $query->select('id', 'title');
                },
                'comment'              => function ($query) {
                    $query->select('id', 'question_id', 'user_id', 'content')->where('parent_id', 0)->orderBy('id');
                },
                'comment.user'         => function ($query) {
                    $query->select('id', 'name');
                },
            ])
            ->where('id', $data['id'])
            ->first();

        $list = Question::query()->inRandomOrder()->limit(10)->get()->toArray();

        return View::make('question.details', ['info' => $info, 'list' => $list]);
    }

    /**
     * 创建
     */
    public function create(Request $request)
    {
        $data = $request->all();

        if (QuestionService::create($data)) {
            throw new Exception('创建失败');
        }
    }
}
